<template>
  <a-card :bordered="false">
    <div class="card-title">{{ $route.meta.title }}</div>
    <div class="table-operator">
      <!-- 搜索板块 -->
      <a-row class="row-item-search">
        <a-form class="search-form" :form="searchForm" layout="inline" @submit="handleSearch">
          <a-form-item label="名称">
            <a-input v-decorator="['name']" placeholder="请输入文件名称" />
          </a-form-item>
          <a-form-item class="search-btn">
            <a-button type="primary" icon="search" html-type="submit">搜索</a-button>
          </a-form-item>
        </a-form>

      </a-row>

      <!-- 操作板块 -->
      <div class="row-item-tab clearfix">
        <a-upload
          name="iFile"
          :accept="'zip'"
          :customRequest="onUpload"
          :multiple="false"
          :showUploadList="false"
        >
          <a-button icon="cloud-upload"
                    class="fl-l"
                    type="primary">上传文件</a-button>
        </a-upload>
        <a-button icon="plus"
                  class="fl-l"
                  @click='onAdd'
                  type="primary">通过文件夹新增</a-button>
      </div>
    </div>
    <s-table
      ref="table"
      rowKey="id"
      :loading="isLoading"
      :columns="columns"
      :data="loadData"
      :pageSize="15"
    >

      <span slot="status" slot-scope="text">
          <a-tag :color='WorkStatusEnum.valueOf(text).color'>{{WorkStatusEnum.getNameByValue(text)}}</a-tag>
      </span>
      <span class="actions" slot="action" slot-scope="text,item">
        <router-link v-if='item.status === WorkStatusEnum.COMPLETED.value'  :to="{ path: '/book/file/list', query: { did: item.id,name:item.name } }">文件列表</router-link>

            <a
              v-if='item.status === 30'
                      @click='onScan(item.id)'
                      v-action:scan
                      >同步文件夹</a>
           <a style='color: red;' v-action:delete @click="handleDelete(item.id)">删除</a>
        <a v-action:edit @click = "handleEdit(item)">编辑</a>
      </span>
    </s-table>
    <add ref='addForm' @handleSubmit='handleRefresh'/>
    <scan ref='scanForm' @handleSubmit='handleRefresh'/>
    <edit ref='editForm' @handleSubmit='handleRefresh'/>
  </a-card>
</template>

<script>
import * as Api from '@/api/book/file'
import { STable } from '@/components'
import WorkStatusEnum from '@/common/enum/workStatus'
import add from './modules/add'
import scan from './modules/scan'
import edit from './modules/edit'
export default {
  name: 'Index',
  components: {
    STable,add,scan,edit

  },
  data () {
    return {
      WorkStatusEnum,
      // 当前表单元素
      searchForm: this.$form.createForm(this),
      // 查询参数
      queryParam: {},
      // 正在加载
      isLoading: false,
      // 表头
      columns: [
        {
          title: '名称',
          dataIndex: 'name'
        },
        {
          title: '文件数量',
          dataIndex: 'file_num'
        },
        {
          title: '状态',
          dataIndex: 'status',
          scopedSlots: { customRender: 'status' }
        },
        {
          title: '创建时间',
          dataIndex: 'create_time'
        },
        {
          title:'操作',
          dataIndex: 'action',
          scopedSlots: {customRender: 'action'}
        }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: param => {
        return Api.list({ ...param, ...this.queryParam })
          .then(response => response.data.list)
      },
      // 会员等级列表
      gradeList: [],
    }
  },
  created () {

  },
  methods: {

    // 确认搜索
    handleSearch (e) {
      e.preventDefault()
      this.searchForm.validateFields((error, values) => {
        if (!error) {
          this.queryParam = { ...this.queryParam, ...values }
          this.handleRefresh(true)
        }
      })
    },

    /**
     * 刷新列表
     * @param Boolean bool 强制刷新到第一页
     */
    handleRefresh (bool = false) {
      this.$refs.table.refresh(bool)
    },


    // 事件: 自定义上传
    onUpload (info) {
      this.isLoading = true
      // 构建上传参数
      const formData = new FormData()
      formData.append('iFile', info.file)
      // 开始上传
      Api.uploadZip(formData)
        .finally(() => {
            this.isLoading = false
            this.handleRefresh(true)
        })
    },
    onAdd(){
      this.$refs.addForm.open()
    },
    onScan(id){
      this.$refs.scanForm.open(id)
    },
    handleEdit(item){
      this.$refs.editForm.open(item)
    },
    handleDelete(id){
      const app = this
      const modal = this.$confirm({
        title: '您确定要删除该记录吗?',
        content: '删除后不可恢复',
        onOk () {
          return Api.delGroup({id})
            .then((result) => {
              app.$message.success(result.message, 1.5)
              app.handleRefresh()
            })
            .finally(result => modal.destroy())
        }
      })
    },
  }
}
</script>

<style lang="less" scoped>
.ant-card-body {
  padding: 22px 29px 25px;
}
</style>
